package com.wonderful.eyepetizer.ui.pages.discover

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.R
import com.wonderful.eyepetizer.data.entity.bean.Category
import com.wonderful.eyepetizer.ui.pages.detail.CategoryDetailActivity

/**
 * Desc: 发现-分类
 * @Author: wonderful
 * @Time: 2024/2/22 12:22
 */

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun CategoryPage(viewModel: DiscoverViewModel = viewModel()) {
    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.categoryRefreshing.value,
        onRefresh = { viewModel.getCategoryList() })
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
    ) {
        LazyVerticalGrid(
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(5.dp),
            columns = GridCells.Fixed(2),
            content = {
                items(viewModel.categoryList) {
                    CategoryItemWidget(category = it)
                }
            })

        PullRefreshIndicator(
            refreshing = viewModel.categoryRefreshing.value,
            state = pullRefreshState,
            modifier = Modifier.align(Alignment.TopCenter)
        )
    }
}

@Composable
fun CategoryItemWidget(category: Category) {
    val context = LocalContext.current
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(5.dp)
            .clickable {
                CategoryDetailActivity.actionStart(
                    context,
                    category.id,
                    category.name,
                    category.headerImage
                )
            },
        contentAlignment = Alignment.Center
    ) {
        Image(
            painter = rememberAsyncImagePainter(
                model = category.headerImage,
                placeholder = painterResource(id = R.mipmap.ic_head_bg)
            ),
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
                .clip(RoundedCornerShape(4.dp)),
            contentScale = ContentScale.Crop
        )
        Text(
            text = "#${category.name}",
            style = TextStyle(fontSize = 22.sp, fontWeight = FontWeight.Bold, color = Color.White),
            textAlign = TextAlign.Center
        )
    }
}